package testLambda;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  测试 lambda 的map 
 *  
 *  lambda的foreach   比  map性能好 1.8倍
 *  
 * @author jinmiao
 * 2016年7月10日
 *
 */
public class Lambda_Map_test {
	public static void main(String[] args) {
		List<LambdaBean> list = LambdaBean.create(10);
//		list = list.stream().filter(id->(id<3)).collect(Collectors.toList());
//		list.forEach(id->{System.out.println(id);});
		
//		lambda(list);
//		normal(list);
		
		//性能测试
		for(int i =0;i<30000000;i++)
		{
			lambda(list);
			normal(list);
		}
		long start = System.currentTimeMillis();
		for(int i =0;i<10000000;i++)
		{
			lambda(list);
		}
		System.out.println(System.currentTimeMillis()-start);
		start = System.currentTimeMillis();
		for(int i =0;i<10000000;i++)
		{
			normal(list);
		}
		System.out.println(System.currentTimeMillis()-start);
		
	}
	
	
	private static void lambda(List<LambdaBean> list)
	{
		List<Integer> res = list.stream().map(bean->{return bean.getI();}).collect(Collectors.toList());
//		res.forEach(i->{System.out.println(i);});
	}
	
	private static void normal(List<LambdaBean> list)
	{
		List<Integer> res = new ArrayList<>(list.size());
		list.forEach(bean->{res.add(bean.getI());});
//		res.forEach(id->{System.out.println(id);});
	}

}
